package com.aspose.cells;

class zgw implements zafu, zahb {
   private int a;
   private int b;
   private Cells c;
   private RowCollection d;
   private zr e;
   private int f = 0;
   private int g;
   private int[][] h;
   private int[] i;
   private int j;
   private Row k;
   private Row l;
   private Row m;
   private int n;

   zgw(Cells var1, RowCollection var2, int var3) {
      this.c = var1;
      this.d = var2;
      this.e = var2.c;
      this.h = new int[this.e.a()][];
      this.j = 0;
      int var4 = -1;
      int var5 = -1;
      if (this.h.length > 1) {
         int var9;
         if (((byte)var3 & 1) != 0) {
            boolean var6 = ((byte)var3 & 2) == 0;
            zahl var7 = this.e.a(-1, -1, false);
            zp var8 = null;

            label128:
            while(true) {
               do {
                  var9 = var7.a();
                  if (var9 < 0) {
                     if (this.j < this.h.length) {
                        int[][] var10 = new int[this.j][];
                        System.arraycopy(this.h, 0, var10, 0, this.j);
                        this.h = var10;
                     }

                     if (this.j > 0) {
                        int var16 = this.j - 1;
                        zahl var11;
                        if (var8.a() > 0) {
                           --var16;
                           var5 = var8.h();
                           var11 = var8.b();

                           while(true) {
                              var9 = var11.a();
                              if (var9 < 0) {
                                 break;
                              }

                              if (var8.h(var9) != 0) {
                                 var4 = var8.h();
                                 break;
                              }
                           }
                        }

                        if (var4 < 0 && var16 > -1) {
                           var8 = this.e.a(this.h[var16]);
                           var5 = var8.h();

                           while(true) {
                              var11 = var8.b();

                              while(true) {
                                 var9 = var11.a();
                                 if (var9 < 0) {
                                    break;
                                 }

                                 if (var8.h(var9) != 0) {
                                    var4 = var8.h();
                                    break;
                                 }
                              }

                              if (var4 > -1) {
                                 break label128;
                              }

                              --var16;
                              if (var16 < 0) {
                                 break label128;
                              }

                              var8 = this.e.a(this.h[var16]);
                           }
                        }
                     }
                     break label128;
                  }

                  var8 = this.e.f(var9);
               } while(!var6 && var8.a() <= 0);

               this.h[this.j++] = (int[])((int[])var8.k());
            }
         } else {
            zahl var12 = this.e.a(-1, -1, true);

            label89:
            do {
               zp var14;
               do {
                  var9 = var12.a();
                  if (var9 < 0) {
                     break label89;
                  }

                  var14 = this.e.f(var9);
                  this.h[this.j++] = (int[])((int[])var14.k());
               } while(var14.a() <= 0);

               if (var5 < 0) {
                  var5 = var14.h();
               }

               zahl var15 = var14.b();

               do {
                  var9 = var15.a();
                  if (var9 < 0) {
                     continue label89;
                  }
               } while(var14.h(var9) == 0);

               var4 = var14.h();
            } while(var4 <= -1);

            for(int var17 = (this.j >> 1) - 1; var17 > -1; --var17) {
               int[] var18 = this.h[var17];
               this.h[var17] = this.h[this.j - 1 - var17];
               this.h[this.j - 1 - var17] = var18;
            }
         }
      }

      this.b = var5;
      this.a = var4;
      this.i = new int[this.h.length];

      for(int var13 = 0; var13 < this.i.length; ++var13) {
         this.i[var13] = -1;
      }

   }

   public int b() {
      return this.j;
   }

   public int c(int var1) {
      int[] var2 = this.h[var1];
      return var2[var2.length - 2];
   }

   public int d(int var1) {
      int[] var2 = this.h[var1];
      return var2[var2.length - 1];
   }

   private zp e(int var1) {
      return this.e.a(this.h[var1]);
   }

   private Row f(int var1) {
      Row var2 = new Row(this.c, this.d, this.e(var1), false);
      var2.b.c(this.i[var1]);
      return var2;
   }

   private Row a(zp var1) {
      return new Row(this.c, this.d, var1, false);
   }

   private zp b(int var1, int var2) {
      zp var3 = this.e.f(var1);
      if (var2 < this.j) {
         System.arraycopy(this.h, var2, this.h, var2 + 1, this.j - var2);
         System.arraycopy(this.i, var2, this.i, var2 + 1, this.j - var2);
      }

      this.h[var2] = (int[])((int[])var3.k());
      ++this.j;
      return var3;
   }

   public int a() {
      return this.b;
   }

   public int a_() {
      return this.a;
   }

   public zp a(int var1) {
      Row var2 = this.b(var1);
      return var2 == null ? null : var2.a;
   }

   public Row b(int var1) {
      Row var2;
      switch(this.n) {
      case 1:
         if (this.k.a.h() == var1) {
            return this.k;
         }

         var2 = this.g(var1);
         if (var2 != null) {
            this.n = 3;
            this.l = var2;
         }

         return var2;
      case 2:
         if (this.k.a.h() == var1) {
            return this.k;
         } else {
            if (this.l.a.h() == var1) {
               this.n = 3;
               return this.l;
            }

            var2 = this.g(var1);
            if (var2 != null) {
               this.n = 8;
               this.m = var2;
            }

            return var2;
         }
      case 3:
         if (this.l.a.h() == var1) {
            return this.l;
         } else {
            if (this.k.a.h() == var1) {
               this.n = 2;
               return this.k;
            }

            var2 = this.g(var1);
            if (var2 != null) {
               this.n = 9;
               this.m = var2;
            }

            return var2;
         }
      case 4:
         if (this.k.a.h() == var1) {
            return this.k;
         } else if (this.l.a.h() == var1) {
            this.n = 6;
            return this.l;
         } else {
            if (this.m.a.h() == var1) {
               this.n = 8;
               return this.m;
            }

            var2 = this.g(var1);
            if (var2 != null) {
               this.n = 8;
               this.m = var2;
            }

            return var2;
         }
      case 5:
         if (this.k.a.h() == var1) {
            return this.k;
         } else if (this.m.a.h() == var1) {
            this.n = 8;
            return this.m;
         } else {
            if (this.l.a.h() == var1) {
               this.n = 6;
               return this.l;
            }

            var2 = this.g(var1);
            if (var2 != null) {
               this.n = 6;
               this.l = var2;
            }

            return var2;
         }
      case 6:
         if (this.l.a.h() == var1) {
            return this.l;
         } else if (this.k.a.h() == var1) {
            this.n = 4;
            return this.k;
         } else {
            if (this.m.a.h() == var1) {
               this.n = 9;
               return this.m;
            }

            var2 = this.g(var1);
            if (var2 != null) {
               this.n = 9;
               this.m = var2;
            }

            return var2;
         }
      case 7:
         if (this.l.a.h() == var1) {
            return this.l;
         } else if (this.m.a.h() == var1) {
            this.n = 9;
            return this.m;
         } else {
            if (this.k.a.h() == var1) {
               this.n = 4;
               return this.k;
            }

            var2 = this.g(var1);
            if (var2 != null) {
               this.n = 4;
               this.k = var2;
            }

            return var2;
         }
      case 8:
         if (this.m.a.h() == var1) {
            return this.m;
         } else if (this.k.a.h() == var1) {
            this.n = 5;
            return this.k;
         } else {
            if (this.l.a.h() == var1) {
               this.n = 7;
               return this.l;
            }

            var2 = this.g(var1);
            if (var2 != null) {
               this.n = 7;
               this.l = var2;
            }

            return var2;
         }
      case 9:
         if (this.m.a.h() == var1) {
            return this.m;
         } else if (this.l.a.h() == var1) {
            this.n = 7;
            return this.l;
         } else {
            if (this.k.a.h() == var1) {
               this.n = 5;
               return this.k;
            }

            var2 = this.g(var1);
            if (var2 != null) {
               this.n = 5;
               this.k = var2;
            }

            return var2;
         }
      default:
         var2 = this.g(var1);
         if (var2 != null) {
            this.n = 1;
            this.k = var2;
         }

         return var2;
      }
   }

   private Row g(int var1) {
      this.f = zkd.a(this, var1, this.f);
      if (this.f > -1) {
         return this.f(this.f);
      } else {
         this.f = -this.f - 1;
         if (this.j == this.h.length) {
            if (this.f == this.j) {
               --this.f;
            }

            return null;
         } else {
            this.g = this.e.a(var1, this.g);
            if (this.g < 0) {
               this.g = -this.g - 1;
               return null;
            } else {
               return new Row(this.c, this.d, this.b(this.g, this.f), false);
            }
         }
      }
   }

   public Cell a(int var1, int var2) {
      Row var3 = this.b(var1);
      if (var3 == null) {
         return null;
      } else {
         Cell var4 = var3.b.a(var2, true, false);
         this.i[this.f] = var3.b.b();
         return var4;
      }
   }

   public zfv a(int var1, zfv var2, int var3) {
      Row var4 = this.b(var1);
      if (var4 == null) {
         return null;
      } else {
         Cell var5 = var4.b.b(var2.a);
         if (var5 != null) {
            var5.b();
            return var5.c;
         } else {
            int var6 = var4.b.d(var2.a);
            return var6 < 0 ? null : var4.a.a(var6, var2, var3);
         }
      }
   }

   public zahr a(int var1, int var2, boolean var3) {
      return (zahr)(var3 ? new zgw.zb(this, var1, var2) : new zgw.zc(this, var1, var2));
   }

   private class zb extends zgw.za {
      zb(zgw var2, int var3, int var4) {
         super(var2, var3, var4, true);
         this.i = this.g;
      }

      protected void d() {
         this.i = this.g;
      }

      public boolean c() {
         this.k = null;
         this.l = null;
         this.g();
         --this.i;
         if (this.j < 0) {
            if (this.i >= this.f) {
               this.h = this.a.c(this.i);
               return true;
            } else {
               return false;
            }
         } else {
            if (this.i >= this.f) {
               if (this.a.e.a(this.j) > this.a.c(this.i)) {
                  ++this.i;
                  this.a.b(this.j, this.i);
                  ++this.g;
               }
            } else {
               this.a.b(this.j, this.f);
               ++this.i;
               ++this.g;
            }

            this.j = this.b.a();
            this.h = this.a.c(this.i);
            return true;
         }
      }

      public void a(int var1) {
         super.a(var1);
         this.i = zkd.a(this.a, var1, this.i);
         if (this.i < 0) {
            this.i = -this.i - 1;
         } else {
            ++this.i;
         }

      }
   }

   private class zc extends zgw.za {
      zc(zgw var2, int var3, int var4) {
         super(var2, var3, var4, false);
         this.i = this.f - 1;
      }

      protected void d() {
         this.i = this.f - 1;
      }

      public boolean c() {
         this.k = null;
         this.l = null;
         this.g();
         ++this.i;
         if (this.j < 0) {
            if (this.i < this.g) {
               this.h = this.a.c(this.i);
               return true;
            } else {
               return false;
            }
         } else {
            if (this.i < this.g) {
               if (this.a.e.a(this.j) < this.a.c(this.i)) {
                  this.a.b(this.j, this.i);
                  ++this.g;
               }
            } else {
               this.a.b(this.j, this.i);
               ++this.g;
            }

            this.j = this.b.a();
            this.h = this.a.c(this.i);
            return true;
         }
      }

      public void a(int var1) {
         super.a(var1);
         this.i = zkd.a(this.a, var1, this.i);
         if (this.i < 0) {
            this.i = -this.i - 2;
         } else {
            --this.i;
         }

      }
   }

   private abstract class za implements zahr {
      protected zgw a;
      protected zahl b;
      protected int c;
      protected int d;
      protected int e;
      protected int f;
      protected int g;
      protected int h;
      protected int i;
      protected int j;
      protected zp k;
      protected Row l;
      private int n;
      private boolean o;

      za(zgw var2, int var3, int var4, boolean var5) {
         this.a = var2;
         this.h = -1;
         this.n = var2.j;
         if (var3 < 0 && var4 < 0) {
            this.e = this.a.h.length;
            this.f = 0;
            this.g = this.n;
            if (this.n < this.e) {
               this.o = false;
               this.b = this.a.e.a(-1, -1, var5);
               this.j = this.b.a();
               if (var5) {
                  this.d = this.a.e.a(this.j);
                  this.c = this.a.e.a(this.a.e.a(-1, false));
               } else {
                  this.c = this.a.e.a(this.j);
                  this.d = this.a.e.a(this.a.e.a(-1, true));
               }
            } else {
               this.o = true;
               this.j = -1;
               this.c = var3;
               this.d = var4;
            }
         } else {
            this.j = -1;
            int var6 = -1;
            int var7 = var3;
            int var8 = var4;
            if (this.n >= this.a.h.length) {
               this.o = true;
            } else {
               this.b = var2.e.b(var3, var4, var5);
               if (this.b == null) {
                  var6 = 0;
               } else {
                  this.j = this.b.a();
                  if (this.j < 0) {
                     var6 = 0;
                  } else {
                     if (var5) {
                        var8 = this.a.e.a(this.j);
                     } else {
                        var7 = this.a.e.a(this.j);
                     }

                     var6 = 1;

                     while(true) {
                        int var9 = this.b.a();
                        if (var9 < 0) {
                           if (var5) {
                              var7 = this.a.e.a(this.j);
                           } else {
                              var8 = this.a.e.a(this.j);
                           }
                           break;
                        }

                        ++var6;
                        this.j = var9;
                     }
                  }
               }
            }

            if (var6 == 0) {
               this.e = 0;
               this.c = var3;
               this.d = var4;
               this.f = -1;
               this.g = -1;
               this.o = true;
            } else {
               this.g = -1;
               if (var3 < 0) {
                  this.f = 0;
               } else {
                  this.f = zkd.a(var2, var3, -1);
                  if (this.f < 0) {
                     this.f = -this.f - 1;
                     if (var4 == var3) {
                        this.g = this.f;
                     }
                  } else if (var4 == var3) {
                     this.g = this.f + 1;
                  }
               }

               if (var4 < 0) {
                  this.g = this.n;
               } else if (this.g < 0) {
                  this.g = zkd.a(var2, var4, -1);
                  if (this.g < 0) {
                     this.g = -this.g - 1;
                  } else {
                     ++this.g;
                  }
               }

               if (var6 < 0) {
                  this.e = this.g - this.f;
                  if (this.e > 1) {
                     var7 = this.a.c(this.f);
                     var8 = this.a.c(this.g - 1);
                  } else if (this.e == 1) {
                     var7 = this.a.c(this.f);
                     var8 = var7;
                  }
               } else {
                  this.e = var6;
                  if (this.g - this.f < this.e) {
                     this.o = false;
                     this.b.b();
                     this.j = this.b.a();
                  } else {
                     this.o = true;
                     this.b = null;
                     this.j = -1;
                  }
               }

               this.c = var7;
               this.d = var8;
            }
         }

      }

      public void g() {
         if (this.a.j != this.n) {
            this.n = this.a.j;
            if (this.o) {
               int var1 = zkd.a(this.a, this.c, this.f) - this.f;
               if (var1 > 0) {
                  this.f += var1;
                  this.g += var1;
                  this.i += var1;
               }
            } else {
               this.f = zkd.a(this.a, this.c, this.f);
               if (this.f < 0) {
                  this.f = -this.f - 1;
               }

               this.g = zkd.a(this.a, this.d, this.g);
               if (this.g < 0) {
                  this.g = -this.g - 1;
               } else {
                  ++this.g;
               }

               if (this.g - this.f == this.e) {
                  this.o = true;
                  this.b = null;
                  this.j = -1;
               }

               if (this.h > -1) {
                  this.i = zkd.a(this.a, this.h, this.i);
               } else {
                  this.d();
               }
            }

         }
      }

      public boolean h() {
         return true;
      }

      public void a(int var1, int var2) {
         com.aspose.cells.a.c.zl.a("Check the logic of using CellsAccessorImmutableCache.RowEnumerator, it should not be synchronized.");
      }

      public boolean hasNext() {
         return this.c();
      }

      public Object next() {
         return this.i();
      }

      public void remove() {
         throw new CellsException(19, "Removing cells data while iteration is not supported");
      }

      public abstract boolean c();

      protected abstract void d();

      public int a() {
         return this.a.d(this.i);
      }

      public void a(int var1) {
         this.h = -1;
         this.g();
         if (!this.o) {
            this.b.b(var1);
            this.j = this.b.a();
         }

         this.k = null;
         this.l = null;
      }

      public void b() {
         this.h = -1;
         this.k = null;
         this.l = null;
         this.g();
         this.d();
         if (!this.o) {
            this.b.b();
            this.j = this.b.a();
         }

      }

      public zp e() {
         if (this.k == null) {
            this.k = this.a.e(this.i);
         }

         return this.k;
      }

      public Row f() {
         if (this.l == null) {
            if (this.k == null) {
               this.k = this.a.e(this.i);
            }

            this.l = this.a.a(this.k);
         }

         return this.a.f(this.i);
      }

      public Object i() {
         return this.f();
      }
   }
}
